;(function (_) {
    $clock.life('#app-body', {
        $webviewListener: {},
        $onloaded: function () {
            this.renderUser();
            this.renderCalender();
        },
        methods: {
            data: {
                calendar: null,
                fromDate: $clock.getDate($clock.hrefParams.date),
                ruleInfo: {},
                errDayInfo: {},
                attendanceInfos: {},
                userInfo: _.member || {}
            },
            getUserOneDayData: function () {
              var component = this;
              $clock.sdk.getUserOneDayData(component.data.fromDate.today, {
                 success: function (res) {
                    component.data.ruleInfo = res.data.ruleInfo || {};
                    component.data.errDayInfo = res.data.errDayInfo || [];
                    component.data.attendanceInfos = res.data.attendanceInfos;
                     component.data.calendar.setOrderD = component.data.errDayInfo;
                     component.render();
                 },
                  error: function () {
                      component.render(true);
                  }
              })
            },
            renderUser: function () {
               var info = this.data.userInfo;
               var userHeader = this.$dom.find('.user-info-zone .user-header');
               var userName = this.$dom.find('.user-info-zone .user-name');
               var userJob = this.$dom.find('.user-info-zone .job-type');
                userHeader.css({
                   backgroundImage: 'url('+(info.iconUrl || '')+')'
                });
                userName.html(info.postName || '');
                userJob.html(info.name || '');
            },
            render: function (isError) {
                var component = this;
                var status = {
                    '0': 'normal',
                    '1': 'late',
                    '2': 'leaveEarly',
                    '3': 'missingClock',
                    '4': 'outside'
                };
                var ruleInfo =  component.data.ruleInfo;
                var list =  component.data.attendanceInfos || [];
                var desText = '';
                var s = '';
              if (ruleInfo.ruleType) {
                  desText += '一天'+(ruleInfo.ruleType)+'次上下班：';
                  if (ruleInfo.times) {
                      desText += ruleInfo.times.join(' ');
                  }
              }
              this.$dom.find('.list-group-title').html(desText);
              if (list.length) {
                  s += ' <div class="result-list-group">\n' +
                      '        <div class="list-group">';
                  for(var i =0;i < list.length;i++) {
                      var item = list[i];
                      var date = $clock.getDate(item.signTime);
                      var fontType = status[item.state] || '';
                      var fontText = $clock.clockTypeMap[fontType] || '';
                      var addressText = '';
                      var addressIcon = 'see-icon-v5-common-location';
                      if (item.sign && item.state !== '3') {
                            if (item.source === '1') {
                               addressIcon = 'see-icon-v5-common-statement'
                            } else if(item.source === '3') {
                                addressIcon = 'see-icon-v5-common-WiFi'
                            }
                          addressText = '<p class="address text-over-ellipsis cmp-font-desc">\n' +
                              '                        <span class="address-icon '+addressIcon+'"></span>\n' +
                              '                        <span class="address-wifi">'+(item.sign||"")+'</span>\n' +
                              '                    </p>';
                      }
                      s+= '<div class="list-item">' +
                          ' <div class="item-content">' +
                          '   <div class="time-status flex-h y-center">' +
                            date.hoursTime+'<span class="cmp-font-desc clock-type-'+(fontType)+'">'+fontText+'</span>' +
                          '   </div>' + addressText+ ' </div></div>'

                  }
                  s+= '</div></div>'
              }
              var listView  = this.$dom.find('#main-view');
              if (s) {
                  listView.html(s);
              } else {
                  _.dialog.loading(false);
                  var loadingType = [
                      {
                          status: "nocontent",
                          text: "未获取到数据",
                      },
                      {
                          status: "systembusy",
                          text: "系统繁忙",
                      }
                  ]
                  _.dialog.loading(_.extend({
                      dom: listView[0],
                      calcHClass: 'list-calcH'
                  }, loadingType[isError ? 1 : 0]));

              }

            },
            renderCalender: function () {
                var today = $clock.getDate();
                var component = this;
                var fromDate = component.data.fromDate;
                component.data.calendar = new _.DateCalender("#month-calendar", {
                    y: fromDate.year,
                    m: fromDate.month,
                    d: fromDate.day,
                    type: 'week',
                    orderD: [],
                    beginYear: today.year - 1,
                    endYear: today.end,
                    callback: function (date) {
                        //do something with date
                        console.log(date)
                        component.data.fromDate = $clock.getDate(date.value);
                        component.getUserOneDayData();
                    }
                });
            }
        }
    })
})(cmp);